﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity;
using VCommons.Encryptor;

namespace Data.Car_Rental
{
    /// <summary>
    /// 用户操作实现类
    /// sqlserver-linq to sql实现
    /// </summary>
    public class UserRepository : Car_RentalRepositoryBase, IUserRepository
    {

        #region IUserRepository 成员

        public VMessage RegisterUser(UserBases entity)
        {
            entity.Password = Utility.EncryptString(entity.Password, Utility.EncryptorType.MD5);
            VMessage vm = new VMessage();
            try
            {
                base.InsertEntity(entity);
                vm.IsComplete = true;
            }
            catch (Exception)
            {
                vm.IsComplete = false;
                throw;
            }
            return vm;

        }

        public void UpdateUser(UserBases entity)
        {
            DB.SubmitChanges();
        }

        public VMessage Login(UserBases entity)
        {
            VMessage vm = new VMessage();
            try
            {
                var entityList = this.GetModel().Where(i => i.Name == entity.Name).ToList();
                if (entityList != null && entityList.Count > 0)
                {
                    entity = entityList.FirstOrDefault(i => i.Password == Utility.EncryptString(entity.Password, Utility.EncryptorType.MD5));

                    if (entity != null)
                    {
                        vm.IsComplete = true;
                        vm.Entity = entity;
                    }
                    else
                    {
                        vm.IsComplete = false;
                        vm.AddItem("用户名密码错误！");

                    }
                }
                else
                {
                    vm.AddItem("用户不存在！");
                }
            }
            catch (Exception)
            {
                vm.IsComplete = false;
                throw;
            }
            return vm;
        }

        public VMessage ChangePassword(UserBases entity)
        {
            VMessage vm = new VMessage();
            try
            {
                vm = this.Login(entity);

                if (vm.IsComplete)
                {
                    entity.Password = Utility.EncryptString(entity.NewPassword, Utility.EncryptorType.MD5);
                    this.Update(entity);
                    entity = vm.Entity as UserBases;
                    vm.IsComplete = true;
                }
                else
                    vm.IsComplete = false;
            }
            catch (Exception)
            {
                vm.IsComplete = false;
                throw;
            }
            return vm;
        }
        #endregion

        #region IRepository<UserBases> 成员

        public void Update(UserBases entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<UserBases> list)
        {
            list.ToList().ForEach(entity => { this.Update(entity); });
        }

        public void Insert(UserBases entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<UserBases> list)
        {
            list.ToList().ForEach(entity => { this.Insert(entity); });
        }

        public UserBases InsertGetIDENTITY(UserBases entity)
        {
            base.InsertEntity(entity);
            return base.Find<UserBases>(entity.UserID);
        }

        public void Delete(UserBases entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<UserBases> list)
        {
            list.ToList().ForEach(entity => { this.Delete(entity); });
        }

        public IQueryable<UserBases> GetModel()
        {
            return base.GetEntities<UserBases>().Select(item => new UserBases_Ext
            {
                CreateDate = item.CreateDate,
                Email = item.Email,
                Name = item.Name,
                Status = item.Status,
                UpdateDate = item.UpdateDate,
                UserID = item.UserID,
                Password = item.Password,
                ConfirmCode = item.ConfirmCode,
                ConfirmPassword = item.ConfirmPassword,
                IDNumber = item.IDNumber,
                IDType = item.IDType,
                QQ = item.QQ,
                Tel = item.Tel,
                TelVerify = item.TelVerify,
            });
        }

        public UserBases Find(params object[] keyValues)
        {
            return base.Find<UserBases>(keyValues);
        }

        #endregion
    }
}
